package com.ttai.ui.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerAcountItemComponet;
import com.ttai.dagger.module.activity.AcountItemPresenterModule;
import com.ttai.presenter.activity.AcountItemPresenter;
import com.ttai.presenter.activity.InsertTipAcountPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.Constant;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AcountItem extends BaseActivity {
    @Bind(R.id.tv_passwordtip)
    EditText tvPasswordtip;
    @Bind(R.id.et_passwordkey)
    EditText etPasswordkey;
    @Bind(R.id.tb_key_visibility)
    ToggleButton tbKeyVisibility;
    @Bind(R.id.btn_jiemi)
    Button btnJiemi;
    @Bind(R.id.tv_myacountname)
    TextView tvMyacountname;
    @Bind(R.id.tv_myaountid)
    TextView tvMyaountid;
    @Bind(R.id.tv_myacountpassword)
    TextView tvMyacountpassword;
    @Bind(R.id.timeclock)
    TextView timeclock;
    @Bind(R.id.btn_copyacount)
    Button btnCopyacount;
    @Bind(R.id.btn_copypassword)
    Button btnCopypassword;
    @Bind(R.id.lljiemi)
    LinearLayout lljiemi;

    @Inject
    AcountItemPresenter acountItemPresenter;

    /**
     * 设置计时器
     */
    private CountDownTimer timer = new CountDownTimer(30000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            timeclock.setText("本界面将在30秒后自动关闭,请尽快登录   " + (millisUntilFinished / 1000) + "秒");
        }

        @Override
        public void onFinish() {
            finish();
        }
    };
    private String aname;
    private String aphone;
    public String secretKey;
    private String value;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acountcontext);
        ButterKnife.bind(this);

        setComponet();

        tbKeyVisibility.setOnCheckedChangeListener(new AcountItem.ToggleButtonClick());

//        acountItemPresenter.dealDB();
        Intent intent=getIntent();
        aname = intent.getStringExtra("aname");
        aphone = intent.getStringExtra("aphone");
        value = intent.getStringExtra("value");

        Log.i("555", "onCreate: tip:"+ value);
        if(value !=null && value.isEmpty()==false){
            tvPasswordtip.setText(value);
            tvPasswordtip.setInputType(InputType.TYPE_NULL);
        }


        tvMyacountname.setText(aname +": ");
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void onStop() {

        super.onStop();
        this.finish();
    }

    @Override
    public void setComponet() {
        DaggerAcountItemComponet.builder()
                .acountItemPresenterModule(new AcountItemPresenterModule(this))
                .build()
                .in(this);
    }

    @OnClick({R.id.btn_jiemi, R.id.btn_copyacount, R.id.btn_copypassword})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_jiemi:
                secretKey =etPasswordkey.getText().toString();
                if(secretKey ==null || secretKey.isEmpty()){
                    Toast.makeText(AcountItem.this,"秘钥不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    lljiemi.setVisibility(View.VISIBLE);

                    /**
                     * 隐藏软键盘
                     */
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

                    String token = Constant.getToken();
                    System.out.println("token:"+token+"aname"+aname);
                    acountItemPresenter.deCode(token,aname);
//                    启动计时器
                }
                break;
            case R.id.btn_copyacount:
                ClipboardManager cmb = (ClipboardManager)AcountItem.this.getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(tvMyaountid.getText().toString().trim()); //将内容放入粘贴管理器,在别的地方长按选择"粘贴"即可
                Toast.makeText(AcountItem.this,"复制账号成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_copypassword:
                ClipboardManager cmb1= (ClipboardManager)AcountItem.this.getSystemService(Context.CLIPBOARD_SERVICE);
                cmb1.setText(tvMyacountpassword.getText().toString().trim()); //将内容放入粘贴管理器,在别的地方长按选择"粘贴"即可
                Toast.makeText(AcountItem.this,"复制账号成功",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @OnClick(R.id.lljiemi)
    public void onViewClicked() {
    }

    public void setText() {
        timer.start();
        tvMyaountid.setText(acountItemPresenter.getXorAcount());
        tvMyacountpassword.setText(acountItemPresenter.getXorPassword());
    }

    private class ToggleButtonClick implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //5、判断事件源的选中状态
            if (isChecked){

                //显示密码
                //etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                etPasswordkey.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                // 隐藏密码
                //etPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                etPasswordkey.setTransformationMethod(PasswordTransformationMethod.getInstance());

            }
            //6、每次显示或者关闭时，密码显示编辑的线不统一在最后，下面是为了统一
            etPasswordkey.setSelection(etPasswordkey.length());
        }
    }
}
